package org.celery.modules.ticket.wrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.celery.modules.shop.entity.Shop;
import org.celery.modules.shop.service.IShopService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.celery.modules.ticket.entity.CustomTicket;
import org.celery.modules.ticket.vo.CustomTicketVO;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单登记表包装类,返回视图层所需的字段
 *
 * @author Celery
 * @since 2023-09-23
 */
public class CustomTicketWrapper extends BaseEntityWrapper<CustomTicket, CustomTicketVO>  {

    public static CustomTicketWrapper build() {
        return new CustomTicketWrapper();
    }

	@Override
	public CustomTicketVO entityVO(CustomTicket customTicket) {
		CustomTicketVO customTicketVO = BeanUtil.copy(customTicket, CustomTicketVO.class);

		return customTicketVO;
	}

	@Override
	public List<CustomTicketVO> listVO(List<CustomTicket> list) {
		List<CustomTicketVO> voList = list.stream().map(this::entityVO).collect(Collectors.toList());
		List<Long> shopIdList = voList.stream().map(CustomTicketVO::getShopId).distinct().collect(Collectors.toList());
		if (Func.isNotEmpty(shopIdList)) {
			IShopService shopService = SpringUtil.getBean(IShopService.class);
			List<Shop> shopList = shopService.list(Wrappers.<Shop>lambdaQuery().in(Shop::getId, shopIdList));

			voList.forEach(item -> shopList.forEach(shop -> {
				if (item.getShopId().equals(shop.getId())) {
					item.setShopName(shop.getResourceOwner());
					item.setShopInfo(shopService.shopUserInfo(shop.getId()));
				}
			}));
		}
		return voList;
	}

}
